package design.simdi.code.final_paper.site.panels.licente.candidati;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import design.simdi.code.final_paper.core.daos.SesiuneDao;
import design.simdi.code.final_paper.core.daos.StudentCandidatDao;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.Sesiune;
import design.simdi.code.final_paper.core.entities.StudentCandidat;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.panels.PagePanel;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;

/**
 * 
 * @author Marius Dumitriu - (Jun 21, 2010 - 3:24:40 PM)
 * 
 */
// @AuthorizeInstantiation( { ApplicationRoles.PROFESSOR,
// ApplicationRoles.ADMIN, ApplicationRoles.STUDENT,
// ApplicationRoles.ABSOLVENT })
public class CandidatiTabPanel extends PagePanel {
	private static final long serialVersionUID = 1L;
	@SpringBean
	private StudentCandidatDao studentCandidatDao;
	@SpringBean
	private SesiuneDao sesiuneDao;
	private DropDownChoice<Sesiune> sesiuniComboBox;
	private WebMarkupContainer tableContainer;

	public CandidatiTabPanel(String id) {
		super(id);
		InjectorHolder.getInjector().inject(this);
		((WicketApplication) getApplication()).setShowErrors(true);
	}

	private SortableCandidatDataProvider sortableCandidatDataProvider;

	@SuppressWarnings("unchecked")
	@Override
	public void panelInit() {
		sortableCandidatDataProvider = new SortableCandidatDataProvider();

		List<Sesiune> lista = getSesiuni();

		sesiuniComboBox = new DropDownChoice<Sesiune>("sesiuni-combo", lista);
		sesiuniComboBox.setNullValid(false);
		if (lista.size() > 0) {
			for (Sesiune s : lista)
				if (s.isDefaulta())
					sesiuniComboBox.setModel(new Model<Sesiune>(s));
			if (sesiuniComboBox.getModelObject() == null)
				sesiuniComboBox.setModel(new Model<Sesiune>(lista.get(0)));
			getSortableCandidatDataProvider().getCriterii().add(
					Restrictions.eq("sesiune.id", sesiuniComboBox.getModelObject().getId()));

		}
		sesiuniComboBox.setOutputMarkupId(true);

		sesiuniComboBox.add(new AjaxFormComponentUpdatingBehavior("onchange") {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				onChangeSesiune(target);
			}
		});

		add(sesiuniComboBox);
		List<IColumn<StudentCandidat>> columns = new ArrayList<IColumn<StudentCandidat>>();

		columns.add(new AbstractColumn<StudentCandidat>(new ResourceModel("name")) {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<StudentCandidat>> cellItem, String componentId,
					IModel<StudentCandidat> rowModel) {
				StudentCandidat sc = rowModel.getObject();
				Person p = getApplicationService().findPersoanaByStudentCandidat(sc);
				if (p == null) {
					cellItem.add(new Label(componentId, "-"));
					return;
				}
				Label l = new Label(componentId, p.toString());
				cellItem.add(l);

			}
		});

		// columns.add(new PropertyColumn(new ResourceModel("nr-matricol"),
		// "student.numarMatricol"));
		columns.add(new AbstractColumn<StudentCandidat>(new ResourceModel("domeniu")) {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<StudentCandidat>> cellItem, String componentId,
					IModel<StudentCandidat> rowModel) {
				StudentCandidat sc = rowModel.getObject();
				Label l = sc.getDomain() == null ? new Label(componentId, "-") : new Label(componentId, sc.getDomain()
						.toString());
				cellItem.add(l);

			}
		});

		columns.add(new AbstractColumn<StudentCandidat>(new ResourceModel("proiect")) {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<StudentCandidat>> cellItem, String componentId,
					IModel<StudentCandidat> rowModel) {
				StudentCandidat sc = rowModel.getObject();
				Label l = sc.getProject() == null ? new Label(componentId, "-") : new Label(componentId, sc
						.getProject().toString());
				cellItem.add(l);

			}
		});
		columns.add(new PropertyColumn(new ResourceModel("status"), "status", "status"));
		sortableCandidatDataProvider.setOwner(getCurrentPerson());
		sortableCandidatDataProvider.setSesiune(sesiuniComboBox.getModelObject());

		MyDataTable table = new MyDataTable<StudentCandidat>("table", columns, sortableCandidatDataProvider,
				getRowsPerPage()) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public ActionToolbar getActionToolbar() {
				return new CandidatiActionToolbar(this);
			}
		};

		tableContainer = new WebMarkupContainer("table-container");
		tableContainer.add(table);
		tableContainer.setOutputMarkupPlaceholderTag(true);

		add(tableContainer);
	}

	protected void onChangeSesiune(AjaxRequestTarget target) {
		sortableCandidatDataProvider.setOwner(getCurrentPerson());
		sortableCandidatDataProvider.setSesiune(sesiuniComboBox.getModelObject());
		target.addComponent(tableContainer);

	}

	private List<Sesiune> getSesiuni() {
		List<Sesiune> lista = new ArrayList<Sesiune>();
		lista = sesiuneDao.findAll(new Order[] { Order.asc("name") });
		return lista;
	}

	public int getRowsPerPage() {
		return 10;
	}

	public void setStudentCandidatDao(StudentCandidatDao studentCandidatDao) {
		this.studentCandidatDao = studentCandidatDao;
	}

	public StudentCandidatDao getStudentCandidatDao() {
		return studentCandidatDao;
	}

	public void setSesiuneDao(SesiuneDao sesiuneDao) {
		this.sesiuneDao = sesiuneDao;
	}

	public SesiuneDao getSesiuneDao() {
		return sesiuneDao;
	}

	public void setSortableCandidatDataProvider(SortableCandidatDataProvider sortableCandidatDataProvider) {
		this.sortableCandidatDataProvider = sortableCandidatDataProvider;
	}

	public SortableCandidatDataProvider getSortableCandidatDataProvider() {
		return sortableCandidatDataProvider;
	}
}
